/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vng.zing.survey.data;

import static com.vng.zing.survey.data.Base.deserialize;
import static com.vng.zing.survey.data.Base.serialize;
import com.vng.zing.survey.thrift.model.Error;
import com.vng.zing.survey.thrift.model.SurveyStatsPerDayItem;
import com.vng.zing.survey.thrift.model.SurveyStatsPerDayKey;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import kyotocabinet.DB;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class SurveyStatsPerDayData extends Base{
     private static final Logger logger_ = Logger.getLogger(IDSurveyData.class);  
    private static final Lock createLock_ = new ReentrantLock();
    private static SurveyStatsPerDayData _instance = null;
    private final DB db;

    public SurveyStatsPerDayData() {
        db = new DB();
        if (!db.open("database/surveystatsperday.kch"+cofigdata, DB.OWRITER | DB.OCREATE|DB.OAUTOTRAN)) {
            System.err.println("open error: " + db.error());
        }

    }

    public static SurveyStatsPerDayData getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new SurveyStatsPerDayData();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }
    public SurveyStatsPerDayItem getSurveyStatsPerDayItem(int idSur, int day) {
        try {
            SurveyStatsPerDayKey surveyStatsPerDayKey = new SurveyStatsPerDayKey(idSur, day);
            byte[] key = serialize(surveyStatsPerDayKey);
            byte[] value = db.get(key);
            if (value == null) {
                return null;
            }
            SurveyStatsPerDayItem dSurveyItem = new SurveyStatsPerDayItem();
            dSurveyItem = (SurveyStatsPerDayItem) deserialize(value);
            return dSurveyItem;
        } catch (IOException  ex) {
            logger_.error("IDSurveyData.getIDSurveyItem:"+ex.getMessage(), ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IDSurveyData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public Error setSurveyStatsPerDayItem(int idSur, int day, SurveyStatsPerDayItem surveyStatsPerDayItem) {

        try {
            SurveyStatsPerDayKey surveyStatsPerDayKey = new SurveyStatsPerDayKey(idSur, day);
            byte[] key = serialize(surveyStatsPerDayKey);
            byte[] value = serialize(surveyStatsPerDayItem);
            if (!db.set(key, value)) {
                return Error.ERR_SYSTEM;
            }
            return Error.SUCCESS;
        } catch (IOException ex) {
              logger_.error("IDSurveyData.setIDSurveyItem:"+ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;
    }
    
}
